package com.sk.cardeal.act.manual;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.BeautyActivity;
import com.sk.cardeal.act.home.activity.BrandChooseActivity;
import com.sk.cardeal.act.home.activity.FixInfoActivity;
import com.sk.cardeal.act.home.fragment.ChooseBrandFragment;
import com.sk.cardeal.adapter.NormalProblemAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.Brand;
import com.sk.cardeal.entity.BrandListBean;
import com.sk.cardeal.entity.Model;
import com.sk.cardeal.entity.NormalProblemList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.HttpAddress;
import com.sk.cardeal.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.sk.cardeal.common.Constants.CHOOSE_CAR_TYPE;

/**
 * Created by sk on 2017/12/14.
 */

public class ManualFragment extends BaseFragment implements HandleDataCallBack {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.maintain_ll)
    LinearLayout maintainLl;
    @BindView(R.id.fix_ll)
    LinearLayout fixLl;
    @BindView(R.id.fix_example)
    ImageView fixExample;
    @BindView(R.id.normal_question)
    LinearLayout normalQuestion;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    Unbinder unbinder;

    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.fragment_manual;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        AppApi.getNormalProblems(this, 2, getActivity());
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @OnClick({R.id.maintain_ll, R.id.fix_ll, R.id.fix_example})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.maintain_ll:
                loadingDialog.show();
                AppApi.getCarBrand(ManualFragment.this, 1, getActivity());
                break;
            case R.id.fix_ll:
                loadingDialog.show();
                AppApi.getCarBrand(ManualFragment.this, 3, getActivity());
                break;
            case R.id.fix_example:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case 10:
                    Model model = data.getParcelableExtra("model");
                    String brandUrl = data.getStringExtra("url");
                    Intent intent = new Intent(mContext, MaintainActivity.class);
                    intent.putExtra("model", model);
                    intent.putExtra("url", brandUrl);
                    startActivity(intent);
                    break;
                case 300:
                    Model model1 = data.getParcelableExtra("model");
                    String brandUrl1 = data.getStringExtra("url");
                    Intent intent1 = new Intent(mContext, FixInfoActivity.class);
                    intent1.putExtra("model", model1);
                    intent1.putExtra("url", brandUrl1);
                    startActivity(intent1);
                    break;
            }
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 1:
                BrandListBean brandListBean = JSON.parseObject(result, BrandListBean.class);
                ArrayList<Brand> brandList = (ArrayList<Brand>) brandListBean.getResultList();
                Intent carIntent = new Intent(mContext, BrandChooseActivity.class);
                carIntent.putParcelableArrayListExtra("data", brandList);
                startActivityForResult(carIntent, CHOOSE_CAR_TYPE);
                break;
            case 2:
                NormalProblemList normalProblemList = JSON.parseObject(result, NormalProblemList.class);
                final List<NormalProblemList.ResultListBean> dataList = normalProblemList.getResultList();
                NormalProblemAdapter adapter = new NormalProblemAdapter(R.layout.item_normal_problem, dataList);
                dataRyc.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(mContext, ProblemDetailsActivity.class);
                        intent.putExtra("id", dataList.get(position).getId());
                        startActivity(intent);
                    }
                });
                break;
            case 3:

                BrandListBean brandListBean1 = JSON.parseObject(result, BrandListBean.class);
                ArrayList<Brand> brandList1 = (ArrayList<Brand>) brandListBean1.getResultList();
                Intent carIntent1 = new Intent(mContext, BrandChooseActivity.class);
                carIntent1.putParcelableArrayListExtra("data", brandList1);
                startActivityForResult(carIntent1, 300);

                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }
}
